/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

public class Option extends ChildbearingPageElement implements IMouseEvents<Option>, IKeyboardEvents<Option>, ILanguageAttributes<Option>, ICoreAttributes {
	private static final String	SELECTED_KEY		= "selected";
	private static final String	CONTENT_BLANK_VALUE	= "";
	private static final String	OPTION_TAG			= "option";
	private static final String	VALUE_KEY			= "value";

	{
		this.setTag(OPTION_TAG);
		this.setSupportsAttributes(true);
		super.addPageElement(new Content(CONTENT_BLANK_VALUE).setElementId(VALUE_KEY));
	}

	private Content				display;

	public String getValue() {
		return this.getAttributeValue(VALUE_KEY);
	}

	public Option setValue(final String _value) {
		return (Option) this.setAttribute(VALUE_KEY, _value);
	}

	@Override
	public String getCssClass() {
		return this.getAttributeValue(CSS_CLASS_KEY);
	}

	@Override
	public String getId() {
		return this.getAttributeValue(ID_KEY);
	}

	@Override
	public String getStyle() {
		return this.getAttributeValue(STYLE_KEY);
	}

	@Override
	public String getTitle() {
		return this.getAttributeValue(TITLE_KEY);
	}

	@Override
	public Option setCssClass(final String _value) {
		return (Option) this.setAttribute(CSS_CLASS_KEY, _value);
	}

	@Override
	public Option setId(final String _value) {
		return (Option) this.setAttribute(ID_KEY, _value);
	}

	@Override
	public Option setStyle(final String _value) {
		return (Option) this.setAttribute(STYLE_KEY, _value);
	}

	@Override
	public Option setTitle(final String _value) {
		return (Option) this.setAttribute(TITLE_KEY, _value);
	}

	@Override
	public Option setOnKeyDown(final String _scriptlet) {
		return (Option) this.setAttribute(KEY_DOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setOnKeyPress(final String _scriptlet) {
		return (Option) this.setAttribute(KEY_PRESS_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setOnKeyUp(final String _scriptlet) {
		return (Option) this.setAttribute(KEY_UP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnKeyDown() {
		return this.getAttributeValue(KEY_DOWN_EVENT_KEY);
	}

	@Override
	public String getOnKeyPress() {
		return this.getAttributeValue(KEY_PRESS_EVENT_KEY);
	}

	@Override
	public String getOnKeyUp() {
		return this.getAttributeValue(KEY_UP_EVENT_KEY);
	}

	@Override
	public Option setDir(final String _value) {
		return (Option) this.setAttribute(DIR_KEY, _value);
	}

	@Override
	public Option setLang(final String _lang) {
		return (Option) this.setAttribute(LANG_KEY, _lang);
	}

	@Override
	public Option setXmlLang(final String _xmlLang) {
		return (Option) this.setAttribute(XML_LANG_KEY, _xmlLang);
	}

	@Override
	public String getDir() {
		return this.getAttributeValue(DIR_KEY);
	}

	@Override
	public String getLang() {
		return this.getAttributeValue(LANG_KEY);
	}

	@Override
	public String getXmlLang() {
		return this.getAttributeValue(XML_LANG_KEY);
	}

	@Override
	public Option setOnClick(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSECLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setOnDblClick(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEDBLCLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setOnMouseDown(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEDOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setOnMouseMove(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEMOVE_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setMouseOut(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEOUT_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setMouseOver(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEOVER_EVENT_KEY, _scriptlet);
	}

	@Override
	public Option setMouseUp(final String _scriptlet) {
		return (Option) this.setAttribute(ON_MOUSEUP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnClick() {
		return this.getAttributeValue(ON_MOUSECLICK_EVENT_KEY);
	}

	@Override
	public String getOnDblClick() {
		return this.getAttributeValue(ON_MOUSEDBLCLICK_EVENT_KEY);
	}

	@Override
	public String getOnMouseDown() {
		return this.getAttributeValue(ON_MOUSEDOWN_EVENT_KEY);
	}

	@Override
	public String getOnMouseMove() {
		return this.getAttributeValue(ON_MOUSEMOVE_EVENT_KEY);
	}

	@Override
	public String getMouseOut() {
		return this.getAttributeValue(ON_MOUSEOUT_EVENT_KEY);
	}

	@Override
	public String getMouseOver() {
		return this.getAttributeValue(ON_MOUSEOVER_EVENT_KEY);
	}

	@Override
	public String getMouseUp() {
		return this.getAttributeValue(ON_MOUSEUP_EVENT_KEY);
	}

	public String getDisplay() {
		if (this.display == null) { return null; }

		return this.display.getContent();
	}

	/**
	 * The property for the display value of the option.
	 * @param _displayText
	 * @return the option on which this method was called.
	 */
	public Option setDisplay(final String _displayText) {
		if (this.display == null) {
			this.display = new Content(_displayText);
		}
		this.removeAllChildren();
		super.addPageElement(this.display);
		return this;
	}

	/**
	 * @throws UnsupportedOperationException caller cannot add children to this element.
	 */
	@Override
	public ChildbearingPageElement addPageElement(final PageElement _element) {
		throw new UnsupportedOperationException("Children cannot be directly added to an option. Set the value instead");
	}

	public void setSelected(final boolean _b) {
		final String attributeValue = this.getAttributeValue(SELECTED_KEY);
		if (attributeValue != null && !_b) {
			this.removeAttribute(SELECTED_KEY);
		} else {
			this.setAttribute(SELECTED_KEY, SELECTED_KEY);
		}
	}
}
